﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI;
using eCRAM.Bll;
using System.Web.Security;
using eCRAM.WebSite.Tools;

namespace eCRAM.WebSite
{
    // Remarque : pour obtenir des instructions sur l'activation du mode classique IIS6 ou IIS7, 
    // visitez http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default", // Nom d'itinéraire
                "{controller}/{action}/{id}", // URL avec des paramètres
                new { controller = "Cram", action = "Index", id = UrlParameter.Optional } // Paramètres par défaut
            );
            

        }

        protected void Session_Start(Object sender, EventArgs e)
        {
            if(Request.IsAuthenticated)
            {
                var login = User.Identity.Name;
                var user = UserManager.GetUserByLogin(login);

                if(user == null || !user. IsActive)
                {
                    FormsAuthentication.SignOut();
                }
                else
                {
                    SessionTools.User = user;
                }
            }
            
        }
        
        protected void Session_End(Object sender, EventArgs e)
        {
         
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterRoutes(RouteTable.Routes);
        }

         
    }
}